home *** CD-ROM | disk | FTP | other *** search
/ Netware Super Library / Netware Super Library.iso / netbios / nbiosrc / nbiosser
Text File  |  1990-10-31  |  9KB  |  244 lines

  1. bunfigs
  2. Figure 1.  Initialize NetBios Driver 
  3.  
  4.   /*--------------------------------------------------------* 
  5.    * Initialize NetBios driver by getting its name, through * 
  6.    * NetBiosEnum() and opening it with NetBiosOpen()        * 
  7.    *--------------------------------------------------------*/ 
  8.   InitializeNetbios(WORD *devHandle) 
  9.   { 
  10.     int ccode; 
  11.     struct netbios_info_1 NB_INFO; 
  12.     unsigned int totEntries, numEntries; 
  13.  
  14.     if (ccode = NetBiosEnum((char far *)"", 1,  
  15.                 (char far *)&NB_INFO, 
  16.                 (unsigned)sizeof(NB_INFO),  
  17.                 (unsigned short far *)&numEntries, 
  18.                 (unsigned short far *)&totEntries)) 
  19.         printf("Error occurred during NetBiosEnum: %x\n", ccode); 
  20.     if (ccode = NetBiosOpen((char far *)NB_INFO.nb1_driver_name,  
  21.                (char far *)0, (unsigned)NB_REGULAR, 
  22.                (unsigned short far *)devHandle)) 
  23.         printf("Error occurred during NetBiosOpen: %x.\n", ccode); 
  24.   }  Figure 2.  Adding a name via the thread AddServerName. 
  25.  
  26.  LONG    wait_sem_val = 0; 
  27.  HSEM    wait_sem = &wait_sem_val; 
  28.   
  29.  main() 
  30.  { 
  31.  . . . . 
  32.     DosSemSet(wait_sem); 
  33.     stackPtr = (WORD far *) malloc(1024) + (1024 / sizeof(WORD)); 
  34.     ccode = DosCreateThread(AddServerName, &addNameThreadID, (char far *)stackPtr); 
  35.     if (ccode) 
  36.         printf("DosCreateThread Error : %d\n", ccode); 
  37.         . . . .  
  38.     DosSemWait(wait_sem, -1L);        /* wait for the name to be added */ 
  39.  
  40.   /*------------------------------------------------------------------------* 
  41.    * AddServerName adds the servers name, clearing the wait_sem when done.  * 
  42.    * It kills itself upon termination, if an error occurs it kills everyone * 
  43.    *------------------------------------------------------------------------*/ 
  44.   void far AddServerName() 
  45.   { 
  46.    if (!AddName(SERVER_NAME, devHandle)) { 
  47.         DeleteName(SERVER_NAME, devHandle); 
  48.         if (!AddName(SERVER_NAME, devHandle)) { 
  49.             DosSemClear(wait_sem); 
  50.             printf("\nError in NetBios, could't add name"); 
  51.             DosExit (1, 0); 
  52.         } 
  53.     } 
  54.     DosSemClear(wait_sem); 
  55.     DosExit (0, 0); 
  56.   } 
  57.  
  58.   /*------------------------------------------------*/ 
  59.   AddName(char *name, WORD devHandle) 
  60.   { 
  61.     NCB addNCB; 
  62.  
  63.     memset((char *)&addNCB, '\0', 64); 
  64.     addNCB.ncb_command = NCBADDNAME; 
  65.     addNCB.ncb_lana_num = 0x00; 
  66.     memcpy(addNCB.ncb_name, name, strlen(name)); 
  67.     if (NetBiosSubmit(devHandle, SINGLE_RETRY, &addNCB)) 
  68.         return(FALSE); 
  69.     else 
  70.         return (TRUE); 
  71. }  Figure 3.  Cloned communications function. 
  72.  
  73. /*----------------------------------------------------------------------* 
  74.  * Receiver is the server side of the IPC connection.  It makes, reads, * 
  75.  * writes, and closes the pipe until 'down' is entered.                 * 
  76.  *----------------------------------------------------------------------*/ 
  77. void far Receiver(int clientNum) 
  78.     COMMBUFFER commBuffer; 
  79.     int ccode; 
  80.  
  81.     DosSemWait(wait_sem, -1L);          /* wait for name to be added */ 
  82.     do { 
  83.         ListenForClient(&sessionNum[clientNum]); 
  84.         do { 
  85.             DoReceive((char *)&commBuffer, sizeof(COMMBUFFER), 
  86.                    sessionNum[clientNum], devHandle, &ncb[clientNum]); 
  87.             if (!TLAServiceRequest(&commBuffer)) 
  88.                 strcpy(commBuffer.record.explain, 
  89.                               "The TLA requested could not be found."); 
  90.             DoSend((char *)&commBuffer, sizeof(COMMBUFFER), 
  91.                    sessionNum[clientNum], devHandle, &ncb[clientNum]); 
  92.             if (commBuffer.lastOne) 
  93.                 break; 
  94.         } while (TRUE); 
  95.         /* client hangs up, so we don't have to */ 
  96.         sessionNum[clientNum] = 0; 
  97.     } while (TRUE); 
  98. Figure 4.  NetBios Listen command. 
  99.  
  100. /*----------------------------------------------------------------------* 
  101.  * Listen for someone to call us, don't return until we've been called  * 
  102.  *----------------------------------------------------------------------*/ 
  103. ListenForClient(int *sessionNum) 
  104.     int ccode; 
  105.     NCB listenNCB; 
  106.  
  107.     memset((char *)&listenNCB, '\0', 64); 
  108.     listenNCB.ncb_command = NCBLISTEN | ASYNCH; 
  109.     strcpy(listenNCB.ncb_callname, "*"); 
  110.     strcpy(listenNCB.ncb_name, SERVER_NAME); 
  111.     listenNCB.ncb_lana_num = 0x00; 
  112.     listenNCB.ncb_rto = 0x30; 
  113.     listenNCB.ncb_sto = 0x10; 
  114.  
  115.     if (ccode = NetBiosSubmit(devHandle, SINGLE_RETRY, &listenNCB)) 
  116.         printf("NCB Submit error. ccode = %x.\n", ccode); 
  117.     while (listenNCB.ncb_cmd_cplt == 0xFF); 
  118.     if (listenNCB.ncb_cmd_cplt == 0) 
  119.         *sessionNum = listenNCB.ncb_lsn; 
  120. Figure 5.  NetBios commands Send and Receive. 
  121.  
  122. /*--------------------------------------------------------*/ 
  123. DoSend(char *buffer, int size, int sessionNum,  
  124.        WORD devHandle, NCB *ncb) 
  125.     int ccode; 
  126.     NCB sendNCB; 
  127.  
  128.     memset( (char *)ncb, '\0', 64 ); 
  129.     ncb->ncb_command = NCBSEND; 
  130.     ncb->ncb_lana_num = 0; 
  131.     ncb->ncb_lsn = sessionNum; 
  132.     ncb->ncb_length = size; 
  133.     ncb->ncb_buffer = buffer; 
  134.     if (ccode = NetBiosSubmit(devHandle, SINGLE_RETRY, ncb)) { 
  135.         printf("Error sending, submit error: %x\n", ccode); 
  136.         return (FALSE); 
  137.       } 
  138.     return (TRUE); 
  139.  
  140. /*--------------------------------------------------------*/ 
  141. DoReceive(char *buffer, int length, int sessionNum,  
  142.           WORD devHandle, NCB *ncb) 
  143.     int ccode; 
  144.  
  145.     memset( (char *)ncb, '\0', 64 ); 
  146.     ncb->ncb_command = NCBRECV; 
  147.     ncb->ncb_lana_num = 0; 
  148.     ncb->ncb_lsn = sessionNum; 
  149.     ncb->ncb_length = length; 
  150.     ncb->ncb_buffer = buffer; 
  151.     if (ccode = NetBiosSubmit(devHandle, SINGLE_RETRY, ncb)) { 
  152.         printf("\nError receiving, submit error: %x\n", ccode); 
  153.         return (FALSE); 
  154.     } 
  155.     return (TRUE); 
  156. } Figure 6.  Close NetBios down. 
  157.  
  158.  /*--------------------------------------------------------* 
  159.  * CleanUp performs the exiting procedures, disconnecting * 
  160.  * and closing open sessions.                             *                                                   
  161.        * 
  162.  *--------------------------------------------------------*/ 
  163. CleanUp() 
  164.     int ccode; 
  165.     int i; 
  166.     char response[20]; 
  167.  
  168.     /*  wait until 'down' is indicated  */ 
  169.     do { 
  170.         scanf("%s", response); 
  171.     } while (strcmp(response, "down") != 0); 
  172.     DosEnterCritSec(); 
  173.     TLACloseIndex(); 
  174.     for (i = 0; i < CLIENT_NUM; i++) { 
  175.         if (sessionNum[i] != 0) { 
  176.             CancelNCB(ncb[i]);     /* cancel outstanding NCB */ 
  177.             DoHangUp(sessionNum[i]); 
  178.         } 
  179.     } 
  180.     DeleteName(SERVER_NAME, devHandle); 
  181.     if (ccode = NetBiosClose(devHandle, (int)0)) 
  182.         printf("Error occurred during NetBiosClose. ccode = %x\n"); 
  183.     DosExit(1, 0); 
  184.     DosExitCritSec(); 
  185.  
  186. Figure 7.  NetBios Call command 
  187.  
  188. /*------------------------------------------------------*/ 
  189. DoCall(char *callName, WORD devHandle, int *sessionNum,  
  190.        char *yourName) 
  191.     NCB callNCB; 
  192.     int ccode; 
  193.  
  194.     memset((char *)&callNCB, '\0', 64); 
  195.     callNCB.ncb_command = NCBCALL; 
  196.     callNCB.ncb_lana_num = 0; 
  197.     callNCB.ncb_rto = 0x10; 
  198.     callNCB.ncb_sto = 0x10; 
  199.     memcpy(callNCB.ncb_callname, callName, strlen(callName)); 
  200.     memcpy(callNCB.ncb_name, yourName, strlen(yourName)); 
  201.     if (ccode = NetBiosSubmit(devHandle, SINGLE_RETRY, &callNCB)) 
  202.         printf("Error calling. NCB Submit error # %x\n", ccode); 
  203.  
  204.    if (callNCB.ncb_cmd_cplt != 0) { 
  205.        printf("\tError in CALL command: %x\n", callNCB.ncb_cmd_cplt); 
  206.        return (FALSE);  
  207.    } 
  208.    *sessionNum = callNCB.ncb_lsn; 
  209.    return (TRUE); 
  210. }  Figure 8.  Commbuffer description. 
  211.  
  212. /* record structure, external data file is a list of these */ 
  213. /* structures, does not have to be sorted                  */ 
  214. typedef struct { 
  215.     char tla[10];      /* the tla of record */ 
  216.     USHORT offset;     /* offset of next record with this TLA */ 
  217.     char expanded[80]; /* the expanded tla */ 
  218.     char explain[160]; /* an explanation of the tla */ 
  219. } RECORD; 
  220.  
  221. /* communication buffer passed into the server module */ 
  222. typedef struct { 
  223.     int type;                /* type of communication - see types below */ 
  224.     RECORD record;           /* information offered */ 
  225.     int lastOne;             /* are there any more requests on this session */ 
  226. } COMMBUFFER;  
  227.  
  228. /* Types of requests that come to server */ 
  229. #define ADD_TO_LIST       0x0001    /* add new tla */ 
  230. #define QUERY             0x0002    /* query to get expanded string only */ 
  231. #define SORT              0x0004    /* sort file and create new index */ 
  232.  
  233.